var rootPath = getRootPath();
var display_style = isPhoneScreen()?"block":"flex";

$(function() {
	var rleId = getUrlParam("rleId");
	if(rleId == '') {
		layer.alert("认证出错,请求参数有误:)");
	}
	$("#rleId").val(rleId);
	
	
	var prevPage = getUrlParam("currentPage");
	$("#role").css("cursor", "pointer");
	
	/*$(".hasDreamCenter").css("display","none");
	$(".noDreamCenter").css("display","none");*/
	/*	initAddress();*/
	//回显
	$.ajax({
		url: rootPath + '/verify/uid/rleId/' + rleId + '/dr/1',
		type: 'GET',
		success: function(data) {
			if(data.returnCode == '0000') {
				var obj = data.returnObject;
				if(obj.asname){
					$("#asid").html("<option value='" + obj.asid + "'>" + obj.asname + "</option>");
				}
				//角色不用匹配 目前只有1个
				if (obj.sex != null && obj.sex != "") {
					$("input:radio[name=sex][value='" + obj.sex + "']")
							.attr("checked", true);
				}
				if (obj.birthday != null && obj.birthday != "") {
					$("#birthday").val(obj.birthday);
				}
				if (obj.email != null && obj.email != "") {
					$("#email").val(obj.email);
				}
				if (obj.province != null && obj.province != "") {
					$("#province option[value='" + obj.province + "']")
							.prop("selected", "selected");
					$("#province").change();
				}
				if (obj.city != null && obj.city != "") {
					$("#city option[value='" + obj.city + "']").prop(
							"selected", "selected");
					$("#city").change();
				}
				if (obj.area != null && obj.area != "") {
					$("#area option[value='" + obj.area + "']").prop(
							"selected", "selected");
				}
				
				if (obj.job != null && obj.job != "") {
					$("#job option[value='" + obj.job + "']").prop(
							"selected", "selected");
				}
				if (obj.subject != null && obj.subject != "") {
					$("#subject").val(obj.subject);
				}
				if (obj.eduAge != null && obj.eduAge != "") {
					$("#eduAge").val(obj.eduAge);
				}
			}
		}
	});
	
	//上一步
	$("#prev").click(function() {
		var creType = $("#sort").val();
		parent.$("#content").prop("src", prevPage + "?rleId="+rleId+"&creType="+creType+"&title=运动梦想课认证");
	});
	
	//申请认证
	$("#applyAuth").click(function() {
		if(!checkData()){
			return;
		}
	
		showLoading();
		
		$("#sportsClassAuthForm").ajaxSubmit({
			url : rootPath + '/verify/saveOrUpdateVerify',
			type: 'POST',
			contentType : 'application/json;charset=UTF-8',
			success : function(data) {
				hideLoading();
				if(data.returnCode == '0000'){
					layer.msg("操作成功",{icon:1,time:1000},function() {	
						var callbackUrl = getUrlParam("callbackUrl");//getUrlParam 带转义
						if(callbackUrl){
							parent.document.location.href = callbackUrl;
						}else{
							parent.$("#content").prop("src","applyResult.html?rleId="+rleId+"&title=运动梦想课认证");
						}
					});
				} else {
					layer.alert(data.returnMessage);
				}
			},
			error: function (jqXHR, textStatus, errorThrown) {
				hideLoading();
				layer.alert("申请失败,请联系管理员");
			}
		});
		return false;
	});
	
	
	 /*$('#hasDreamCenter').change(function () {
		 hideMsg("hasDreamCenter");
	        if ($("#hasDreamCenter").val()=='Y') {
	        	$(".hasDreamCenter").css("display",display_style);
	        	$(".noDreamCenter").css("display","none");
	        }else if ($("#hasDreamCenter").val()=='N') {
	        	$(".hasDreamCenter").css("display","none");
	        	$(".noDreamCenter").css("display",display_style);
	        }else{
	        	$(".hasDreamCenter").css("display","none");
	        	$(".noDreamCenter").css("display","none");
	        }
	    });*/
	 $("input[name='sex']").change(function(){
		 hideMsg("sex");
	 });
	 $('#isSports').change(function () {
		 hideMsg("isSports");
	 });
	 
	 //去修改手机号
	 $("#editPhone").click(function(){
		 parent.$("#content").prop("src","modifyPhone.html");
	 })
});

function checkData(){
	var sex = $("input[name='sex']:checked").val();
	if(!sex || $.trim(sex) == '') {
		$("#sex").focus();
		showErrMsg("sex");
		return;
	}else{
		hideMsg("sex");
	}
	if($.trim($("#birthday").val()) == '') {
		$("#birthday").focus();
		showErrMsg("birthday");
		return;
	}else{
		hideMsg("birthday");
	}
	if($.trim($("#phone").val()) == '') {
		$("#phone").focus();
		showErrMsg("phone");
		return;
	}else{
		hideMsg("phone");
	}
	if($.trim($("#email").val()) == '') {
		$("#email").focus();
		$("#emailTip").text("请输入邮箱");
		showErrMsg("email");
		return;
	}else if(!checkEmail($("#email").val())){
		$("#emailTip").text("请输入有效的邮箱地址");
		showErrMsg("email");
		return;
	} else {
		hideMsg("email");
	}
	
/*	if($.trim($("#province").val()) == '' 
		|| $.trim($("#city").val()) == '' 
			|| $.trim($("#area").val()) == '') {
		showErrMsg("pca");
		return;
	}else{
		hideMsg("pca");
	}*/
	
	var asid=$("#asid option:selected").val();
	if($.trim(asid) == '') {
		$("#asid").focus();
		showErrMsg("asid");
		return false;
	}else{
		hideMsg("asid");
	}
	
	if($.trim($("#job").val()) == '') {
		$("#job").focus();
		showErrMsg("job");
		return;
	}else{
		hideMsg("job");
	}
	if($.trim($("#subject").val()) == '') {
		$("#subject").focus();
		showErrMsg("subject");
		return;
	}else{
		hideMsg("subject");
	}
	if($.trim($("#eduAge").val()) == '') {
		$("#eduAge").focus();
		showErrMsg("eduAge");
		return;
	}else{
		hideMsg("eduAge");
	}
	return true;
}
